#include <QApplication>

#include "filesviewdelegate.h"
#include "fileserver.h"

FilesViewDelegate::FilesViewDelegate(FileServer *parent) :QItemDelegate(parent)
{
}

void FilesViewDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
    if(index.column() != 1)
    {
        QItemDelegate::paint(painter, option, index);
        return;
    }

    QStyleOptionProgressBar progressBar;

    progressBar.state = QStyle::State_Enabled;
    progressBar.direction = QApplication::layoutDirection();
    progressBar.rect = option.rect;
    progressBar.fontMetrics = QApplication::fontMetrics();
    progressBar.minimum = 0;
    progressBar.maximum = 100;
    progressBar.textAlignment = Qt::AlignCenter;
    progressBar.textVisible = true;

    int progress = qobject_cast<FileServer *>(parent())->progress(index.row());
    progressBar.progress = progress;

    progressBar.text = QString().sprintf("%d%%",progressBar.progress);

    QApplication::style()->drawControl(QStyle::CE_ProgressBar, &progressBar, painter);
}
